package gui.panels;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

import mysql.MoviesQueries;
import mysql.MusicQueries;
import mysql.PersonQueries;
import objects.Music;
import objects.Person;

public class CheckoutMusic extends JFrame implements ActionListener {

	public Music mcurrentEntry;
	public MusicQueries mmusicQueries;
	public List<Music> mResults;
	public int mnumberOfEntries = 0;
	public int mCurrentEntryIndex = 0;
	
	public Person pcurrentEntry;
	public PersonQueries ppersonsQueries;
	public List<Person> pResults;
	public int pnumberOfEntries = 0;
	public int pCurrentEntryIndex = 0;
	
	private JLabel musicLabel;
	public JComboBox musicCombo;
	private JLabel userLabel;
	public JComboBox userCombo;
	public JButton chout;
	
	public CheckoutMusic() {
		musicLabel = new JLabel("Music: ");
		userLabel = new JLabel("User: ");
		chout = new JButton("CheckOut");
		chout.addActionListener(this);
		setLayout(new GridLayout(3,2,4,4));
		getMovies();
		getPersons();
		add(musicLabel);
		add(musicCombo);
		add(userLabel);
		add(userCombo);
		add(chout);
		pack();
		setVisible(true);
	}
	
	private void getMovies() {
		mmusicQueries = new MusicQueries();
		mResults = mmusicQueries.getAllMusic();
		String[] comboArray = new String[mResults.size()];
		for (int i=0;i<mResults.size();i++) {
			comboArray[i] = mResults.get(i).getAlbum();
		}
		musicCombo = new JComboBox(comboArray);
	}
	
	private void getPersons() {
		ppersonsQueries = new PersonQueries();
		pResults = ppersonsQueries.getAllPeople();
		String[] comboArray = new String[pResults.size()];
		for (int i=0;i<pResults.size();i++) {
			comboArray[i] = pResults.get(i).getLastName();
		}
		userCombo = new JComboBox(comboArray);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == this.chout) {
			int moid = musicCombo.getSelectedIndex() + 401;
			if (mResults.get(musicCombo.getSelectedIndex()).isOut() == 1) {
				System.out.println("Music already out");
			}
			else { 
				System.out.println(moid);
				mmusicQueries.checkoutMovie(moid, 1);
			}
		}
	}

}
